<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="人物名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入人物名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="qq" prop="qq">
        <el-input
          v-model="queryParams.qq"
          placeholder="请输入qq"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信" prop="wx">
        <el-input
          v-model="queryParams.wx"
          placeholder="请输入微信"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否解雇" prop="isFire">
        <el-select v-model="queryParams.isFire" placeholder="请选择是否解雇" clearable size="small">
          <el-option
            v-for="dict in isFireOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生日" prop="birth">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.birth"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="身份证号" prop="idNo">
        <el-input
          v-model="queryParams.idNo"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:people:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:people:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:people:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:people:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="peopleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="人物名称" align="center" prop="userName" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="分类逗号隔开" v-if="false" align="center" prop="category" />
      <el-table-column label="分类" align="center" prop="categoryName" />
      <el-table-column label="人物描述" align="center" prop="detail" />
      <el-table-column label="个人照片" :show-overflow-tooltip="true" align="center" prop="albumId" />
      <el-table-column label="作品集合" :show-overflow-tooltip="true" align="center" prop="workAlbumId" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="qq" align="center" prop="qq" />
      <el-table-column label="微信" align="center" prop="wx" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="头像" :show-overflow-tooltip="true" align="center" prop="mainPhoto" />
      <el-table-column label="是否解雇" align="center" prop="isFire" :formatter="isFireFormat" />
      <el-table-column label="专业" align="center" prop="pro" />
      <el-table-column label="生日" align="center" prop="birth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idNo" />
      <el-table-column label="家庭住址" align="center" prop="address" />
      <el-table-column label="目前住址" align="center" prop="nowAddress" />
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:people:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:people:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改平台相关的人物对话框 -->
    <el-dialog :title="title" :visible.sync="open"  append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="人物名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入人物名称" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="分类逗号隔开" prop="category">
          <el-select
            v-model="form.category"
            multiple
            filterable
            allow-create
            default-first-option
            placeholder="请选择分类">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人物描述" prop="detail">
          <el-input v-model="form.detail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="个人照片" prop="poster">
          <el-upload
            :headers="videoUpload.headers"
            :action="videoUpload.url"
            :file-list="albumId"  
            list-type="picture-card"
            :before-upload="limit_9"
            :on-success="t1s"
            :on-preview="handlePictureCardPreview"
            :on-remove="t1r"
            accept=".jpg,.jpeg,.png,,.JPG,.JPEG"
            :limit="9">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="作品集合" prop="imageId">
          <el-upload
            :headers="videoUpload.headers"
            :action="videoUpload.url"
            :file-list="workAlbumId"   
            list-type="picture-card"
            :before-upload="limit_10"
            :on-success="t2s"
            :on-preview="handlePictureCardPreview"
            :on-remove="t2r"
            accept=".jpg,.jpeg,.png,,.JPG,.JPEG"
            :limit="10">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible" class="my-dialog">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="qq" prop="qq">
          <el-input v-model="form.qq" placeholder="请输入qq" />
        </el-form-item>
        <el-form-item label="微信" prop="wx">
          <el-input v-model="form.wx" placeholder="请输入微信" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="头像" prop="poster">
          <el-upload
            :headers="videoUpload.headers"
            :action="videoUpload.url"   
            :file-list="mainPhoto"
            list-type="picture-card"
            :before-upload="limit_1"
            :on-success="t3s"
            :on-preview="handleMainPhotoPreview"
            :on-remove="t3r"
            accept=".jpg,.jpeg,.png,,.JPG,.JPEG"
            :limit="1">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否解雇">
          <el-radio-group v-model="form.isFire">
            <el-radio
              v-for="dict in isFireOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="专业" prop="pro">
          <el-input v-model="form.pro" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item label="生日" prop="birth">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.birth"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNo">
          <el-input v-model="form.idNo" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="目前住址" prop="nowAddress">
          <el-input v-model="form.nowAddress" placeholder="请输入目前住址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listPeople, getPeople, delPeople, addPeople, updatePeople, exportPeople } from "@/api/content/people";
  import { listCategory } from "@/api/content/category";
  import { getToken } from '@/utils/auth'
  export default {
    name: "People",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 平台相关的人物表格数据
        peopleList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否解雇字典
        isFireOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: undefined,
          nickname: undefined,
          category: undefined,
          detail: undefined,
          phone: undefined,
          qq: undefined,
          wx: undefined,
          email: undefined,
          mainPhoto: undefined,
          isFire: undefined,
          pro: undefined,
          birth: undefined,
          idNo: undefined,
          address: undefined,
          nowAddress: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          userName: [
            { required: true, message: "人物名称不能为空", trigger: "blur" }
          ],
          category: [
            { required: true, message: "分类逗号隔开不能为空", trigger: "blur" }
          ],
          phone: [
            { required: true, message: "手机号不能为空", trigger: "blur" }
          ],
          mainPhoto: [
            { required: true, message: "头像", trigger: "blur" }
          ],
          isFire: [
            { required: true, message: "是否解雇不能为空", trigger: "blur" }
          ],
          pro: [
            { required: true, message: "专业不能为空", trigger: "blur" }
          ],
          birth: [
            { required: true, message: "生日不能为空", trigger: "blur" }
          ],
          idNo: [
            { required: true, message: "身份证号不能为空", trigger: "blur" }
          ],
          address: [
            { required: true, message: "家庭住址不能为空", trigger: "blur" }
          ],
          nowAddress: [
            { required: true, message: "目前住址不能为空", trigger: "blur" }
          ]
        },
        videoUpload:{
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/sys/file/upload",
          url2: process.env.VUE_APP_BASE_API,
        },
        dialogImageUrl: '',
        dialogVisible: false,
        workAlbumId : [],
        albumId:[],
        mainPhoto:[],
        categoryList:[],
        t1:[],
        t2:[],
        t3:[],
      };
    },
    created() {
      this.getList();
      this.getDicts("plat_fire_status").then(response => {
        this.isFireOptions = response.data;
      });
      this.getCategoryList()
    },
    methods: {
      /** 查询平台相关的人物列表 */
      getList() {
        this.loading = true;
        listPeople(this.queryParams).then(response => {
          this.peopleList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getCategoryList() {
        this.loading = true;
        listCategory(this.queryParams).then(response => {
          this.categoryList = response.rows;
          // console.log(this.categoryList)
          this.total = response.total;
          this.loading = false;
        });
      },
      // 是否解雇字典翻译
      isFireFormat(row, column) {
        return this.selectDictLabel(this.isFireOptions, row.isFire);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          userName: undefined,
          nickname: undefined,
          category: undefined,
          detail: undefined,
          albumId: undefined,
          workAlbumId: undefined,
          userId: undefined,
          phone: undefined,
          qq: undefined,
          wx: undefined,
          email: undefined,
          mainPhoto: undefined,
          isFire: 0,
          pro: undefined,
          birth: undefined,
          idNo: undefined,
          address: undefined,
          nowAddress: undefined,
        };
          this.workAlbumId  =[];
          this.mainPhoto=[];
          this.albumId=[]
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.getCategoryList()
        this.open = true;
        this.title = "添加平台相关的人物";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getCategoryList()
        const id = row.id || this.ids
        getPeople(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改平台相关的人物";
          this.form.category= response.data.platCategory

          this.albumId = JSON.parse(response.data.albumId).map(item => {
            return {
              url: item
            }
          });
          this.workAlbumId = JSON.parse(response.data.workAlbumId).map(item => {
            return {
              url: item
            }
          });
          this.mainPhoto = JSON.parse(response.data.mainPhoto).map(item => {
            return {
              url: item
            }
          });
//t1 albumId  t2  workAlbumId  t3 mainPhoto
           this.t1 = JSON.parse(response.data.albumId);
           this.t2 = JSON.parse(response.data.workAlbumId);
           this.t3 = JSON.parse(response.data.mainPhoto);

        });
      },
      /** 提交按钮 */
      submitForm: function() {
        console.log(this.form)
        console.log(JSON.stringify(this.form.category))
        if(this.t3.length==0){
        this.msgError("主图不能为空");
        return
        }
        if(this.t2.length==0){
        this.msgError("作品不能为空");
        return
        }
        if(this.t1.length==0){
        this.msgError("个人照片不能为空");
        return
        }
                console.log("sss")
           var ca = new Array();
           console.log(this.form.category)
          for(var i = 0;i<this.form.category.length;i++){
                    ca.push(this.form.category[i])
          }
        this.$refs["form"].validate(valid => {
          if (valid) {
            console.log(this.t1)
            console.log(this.t2)
            console.log(this.t3)
            this.form.albumId = JSON.stringify(this.t1);
            this.form.workAlbumId = JSON.stringify(this.t2);
           this.form.mainPhoto = JSON.stringify(this.t3);
            this.form.category = JSON.stringify(ca);
            console.log(this.form)
            if (this.form.id != undefined) {
              updatePeople(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addPeople(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除平台相关的人物编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPeople(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有平台相关的人物数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPeople(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },
    
      t1s(res, file){
         console.log(file)
       this.albumId.push(file)
       this.t1.push(file.response.url)
      },
      t2s(res, file){
         console.log(file)
     this.workAlbumId.push(file)
     this.t2.push(file.response.url)
      },
      t3s(res, file){
         console.log(file)
        this.mainPhoto.push(file)
         this.t3.push(file.response.url)
      },
    
       t1r(file, fileList){
          console.log(fileList)
       this.albumId = fileList;
       this.t1 = [];
       for(var i= 0;i<fileList.length;i++){
            this.t1.push(fileList[i].url)
        }
       },
       
       t2r(file, fileList){
          console.log(fileList)
          this.workAlbumId = fileList
           this.t2 = [];
          for(var i= 0;i<fileList.length;i++){
          this.t2.push(fileList[i].url)
          }
       },     
       t3r(file, fileList){
         console.log(fileList)
          this.mainPhoto = fileList
           this.t3=[];
          for(var i= 0;i<fileList.length;i++){
             this.t3.push(fileList[i].response.url)
          }
       },
      limit_1(file){
        this.mainPhoto.length>1;
        this.msgInfo("不能大于1张")
      },
      limit_9(file){
        this.mainPhoto.length>9;
        this.msgInfo("不能大于9张")
      },
      limit_10(file){
        this.mainPhoto.length>10;
        this.msgInfo("不能大于10张")
      },
      handleMainPhotoPreview(){

      },
      handlePictureCardPreview(){

      }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss">
  .my-dialog{
    margin-left: 56%;
  }

</style>
